#ifndef PACKETREADERWRITER_H
#define PACKETREADERWRITER_H

#include "core/Constans.h"
#include "core/Node.h"
#include "packet/AbstractPacket.h"

#include <QImage>
#include <QByteArray>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>

class PacketReaderWriter
{
public:
    PacketReaderWriter();

    void writePacketData(const AbstractPacket * packet, QByteArray & array);
    AbstractPacket * readPacket(QByteArray & array);

private:
    void writePacket(const AbstractPacket * packet);
    AbstractPacket * readPacket(PacketType packetType);

    void writeNode(const Node & node, const char * nodeElementName = "node");
    PacketType getPacketType(const QString & packetTypeString);

    QString formatString(quint16 value);

    QString * readTextElement(const QString & name, const QString & errorMsg);
    Node * readNode(const QString & name = "node", const QString & errorMsg = "");

    QColor readColor();
    void writeColor(QColor color);

    void writeImage(const QImage & image);
    void readImage(QImage & image);

    void riseReadError(const QString & errorMsg);
    template <typename T> void deletePtr(T * ptr);

    QXmlStreamReader xmlReader;
    QXmlStreamWriter xmlWriter;
};

#endif // PACKETREADERWRITER_H
